package com.cryptape.cita_wallet.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.MapUtils;
import com.blankj.utilcode.util.SPUtils;
import com.cryptape.cita_wallet.bean.VersionUpdateBean;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.language.LanguageType;
import com.cryptape.cita_wallet.language.MultiLanguageUtil;
import com.cryptape.cita_wallet.net.callback.CommonCallback;
import com.cryptape.cita_wallet.net.request.HttpRequestMode;
import com.cryptape.cita_wallet.service.http.TokenService;
import com.cryptape.cita_wallet.util.HProgressDialogUtils;
import com.cryptape.cita_wallet.util.RSAUtils;
import com.cryptape.cita_wallet.util.RootUtil;
import com.cryptape.cita_wallet.util.UpdateAppHttpUtil;
import com.cryptape.cita_wallet.util.db.SharePrefUtil;
import com.mangoworld.android.R;
import com.vector.update_app.UpdateAppBean;
import com.vector.update_app.UpdateAppManager;
import com.vector.update_app.service.DownloadService;

import java.io.File;
import java.util.Locale;
import java.util.Map;

import rx.Subscriber;

;

/**
 * Created by duanyytop on 2018/6/12
 */
public class SplashActivity extends BaseActivity {

    public static final String EXTRA_FIRST = "extra_first";
    public static final String LOCK_TO_MAIN = "lock_to_main";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()) {
            finish();
            return;
        }
//        setContentView(R.layout.activity_splash);

//        PermissionRequest request = AndPermission.with(SplashActivity.this).runtime().permission(Permission.Group.STORAGE);
//        request.rationale(new RuntimeRationale()).onGranted(permissions -> {
//        });
//        request.onDenied(permissions -> {
//            PermissionUtil.showSettingDialog(SplashActivity.this, permissions);
//        });
//        request.start();

        //默认为正式模式
        boolean mode = SPUtils.getInstance(ConstantUtil.MANGO_WORLD_PREFERENCE_OTHER_INFO).getBoolean(ConstantUtil.SP_MODE_CHANGE, false);
        ConstantUtil.isTestMode = mode;

        try {
            Map params = MapUtils.newHashMap();
            params.put("registrationId", SharePrefUtil.getRegistrationID());
            String jsonData = GsonUtils.toJson(params);
            String content = RSAUtils.encrypt(jsonData);
            HttpRequestMode.getCurrentLanguage(SplashActivity.this, content, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        inLoginPage = true;
//        getVersion();
        if (RootUtil.isDeviceRooted()) {
            new AlertDialog.Builder(mActivity)
                    .setTitle(R.string.safe_hint)
                    .setMessage(R.string.root_hint)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();
                        }
                    }).setNegativeButton(R.string.go_on, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    gotoMainPage();
                }
            }).setCancelable(false)
                    .create().show();
        } else {
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    try {
                        sleep(1000);
                        gotoMainPage();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }

        initLanguage();
    }

    private void gotoMainPage() {
        if (!TextUtils.isEmpty(SharePrefUtil.getCurrentWalletName())) {
            if (SharePrefUtil.getBoolean(ConstantUtil.FINGERPRINT, false)) {
                Intent intent = new Intent(mActivity, FingerPrintActivity.class);
                intent.putExtra(LOCK_TO_MAIN, true);
                startActivity(intent);
            } else {
                Log.e("SplashActivity", "goto MainActivity");
                startActivity(new Intent(mActivity, MainActivity.class));
            }
        } else {
            Log.e("SplashActivity", "goto AddWalletActivity");
            Intent intent = new Intent(mActivity, AddWalletActivity.class);
            startActivity(intent);
        }
        finish();
    }

    private void initLanguage() {
//        LanguageKeeper.getInstance().attachBaseContext(this);
//        LanguageKeeper.attachBaseContext(this);
//        LanguageKeeper keeper = new LanguageKeeper(this);
//        LanguageKeeper.attachBaseContext(this,keeper.getLanguage());
//        switch (keeper.getLanguage()) {
//            case LanguageKeeper.LANGUAGE_CN:
//                changeAppLanguage(Locale.CHINA);
//                break;
//            case LanguageKeeper.LANGUAGE_EN:
//                changeAppLanguage(Locale.ENGLISH);
//                break;
//            case LanguageKeeper.LANGUAGE_JA:
//                changeAppLanguage(Locale.JAPAN);
//                break;
//            case LanguageKeeper.LANGUAGE_KO:
//                changeAppLanguage(Locale.KOREA);
//                break;
//            default:
//                Locale locale;
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//                    locale = LocaleList.getDefault().get(0);
//                } else {
//                    locale = Locale.getDefault();
//                }
//                String str = locale.getLanguage();
//                if (!TextUtils.isEmpty(str) && str.toLowerCase().contains("en")) {
//                    changeAppLanguage(Locale.ENGLISH);
//                }else {
//                    changeAppLanguage(Locale.CHINA);
//                }
//                break;
//        }
    }

//    @Override
//    protected void attachBaseContext(Context base) {
//        Context myContext = LanguageKeeper.getInstance().attachBaseContext(base);
//        super.attachBaseContext(myContext);
//    }

    public void changeAppLanguage(Locale locale) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Configuration configuration = getResources().getConfiguration();
            configuration.setLocale(locale);
        } else {
            DisplayMetrics metrics = getResources().getDisplayMetrics();
            Configuration configuration = getResources().getConfiguration();
            configuration.locale = locale;
            getResources().updateConfiguration(configuration, metrics);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
